package sg.edu.nus.iss.team2s.jass.dao;

/**
 * @author Zhang Jinzhong
 */

import java.util.Iterator;
import java.util.List;

import sg.edu.nus.iss.team2s.jass.dto.User;

public class UserDAO extends AbstractPropertiesDAO {
	public static final String FILENAME = "User";
	@Override
	public void add(Object obj) throws DAOException {
		/*
		 * This is method is left unimplemented as the maintenance of User is out of
		 * scope. 
		 */
		throw new DAOException("adding User is not supported in this version!");
	}

	@Override
	public Object get(String key) throws DAOException {
		String[] keyvalue = key.split("\\|");
		if(keyvalue.length == 2)
		{
			List<Object> jobSeekerList = list();
			Iterator<Object> iterator = jobSeekerList.iterator();
			while(iterator.hasNext()){
				User user = (User)iterator.next();
				String loginId = user.getLoginName();
				String password = user.getPassword();
				if(loginId.equalsIgnoreCase(keyvalue[0]) && password.equalsIgnoreCase(keyvalue[1])){
					return user;
				}
			}
		}
		return null;
	}

	@Override
	public List<Object> list() throws DAOException {
		List<Object> list = retrieve(FILENAME);
		return list;
	}

	@Override
	public void update(List<Object> list) throws DAOException {
		/*
		 * This is method is left unimplemented as the maintenance of User is out of
		 * scope. 
		 */
		throw new DAOException("updating User is not supported in this version!");
	}

}
